﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace PIM_ATE
{
    public class ClsDataUser
    {
        //用户信息
        public string sUser { get { return aUser.user; } }
        public string sUserID { get { return aUser.userid; } }
        public string sRemark { get { return aUser.remark; } }
        public string sUser2 { get { return aUser2.user; } }
        public string sUserID2 { get { return aUser2.userid; } }
        public string sRemark2 { get { return aUser2.remark; } }
        public string sDarkroomNum { get; set; }
        public string sError { get; set; }
        public string sEquID { get; set; }

        //数据特采
        public ClsDataSpecial aSpecial;

        public clsUser aUser;
        public clsUser aUser2;
        string m_sConnect;
        string m_sTableName;

        public ClsDataUser()
        {
            if (GDefine.RUNNING_MODE == 0)
                m_sConnect = GDefine.DBNamePIM;
            else
                m_sConnect = GDefine.DBNameMOD;

            m_sTableName = "tb_User";
            aUser = new clsUser(m_sConnect);
            aUser2 = new clsUser(m_sConnect);
            aSpecial = new ClsDataSpecial();
            aSpecial.bEnable = false;
            aSpecial.bNeedCheckPIM = true;
            aSpecial.fMinVaule = -200;
            aSpecial.fMaxVaule = 0;
        }

        public bool verifyUser(string usrid, string pwd, string usrid2 = null, string pwd2 = null)
        {
            if (!verifyUser1(usrid, pwd, aUser))
            {
                return false;
            }
            if (usrid2 != null && pwd2 != null)
            {
                if (!verifyUser1(usrid2, pwd2, aUser2))
                {
                    return false;
                }
            }
            return true;


            //aUser.userid = usrid;
            //if (GDefine.IS_USER_NOT_LOGIN) return true;
            //if (aUser.userid == "999999" && pwd == "999999")
            //{
            //    aUser.power = "999";
            //    return true;
            //}

            //int number = -1;
            //if (!int.TryParse(usrid, out number))
            //{
            //    sError = "员工号需要数字!";
            //    return false;
            //}

            //try
            //{
            //    SqlParameter param_id = new SqlParameter("@id", usrid);
            //    string sSql = string.Format("select * from [{0}] where UserID = @id", m_sTableName);
            //    DataTable dtUser = MicroDBHelper.ExecuteDataTable(sSql, new SqlParameter[] { param_id }, m_sConnect);
            //    if (dtUser == null || dtUser.Rows.Count == 0)
            //    {
            //        sError = "员工号不存在!";
            //        return false;
            //    }

            //    if (dtUser.Rows[0][clsUser.chn_Pwd].ToString() == pwd)
            //    {
            //        aUser.userid = dtUser.Rows[0][clsUser.chn_UserID].ToString();
            //        aUser.user = dtUser.Rows[0][clsUser.chn_User].ToString();
            //        aUser.password = dtUser.Rows[0][clsUser.chn_Pwd].ToString();
            //        //if (!int.TryParse(dtUser.Rows[0][clsUser.chn_Power].ToString(), out aUser.power))
            //        //    aUser.power = 0;
            //        aUser.power = dtUser.Rows[0][clsUser.chn_Power].ToString();
            //        aUser.remark = dtUser.Rows[0][clsUser.chn_Remark].ToString();

            //        return true;
            //    }

            //    sError = "密码错误!";
            //}
            //catch (Exception ex)
            //{
            //    //System.Windows.Forms.MessageBox.Show("服务器异常!");
            //    sError = "服务器异常!" + ex.Message;
            //}

            //return false;
        }

        bool verifyUser1(string usrid, string pwd, clsUser user)
        {
            user.userid = usrid;
            if (GDefine.IS_USER_NOT_LOGIN) return true;
            if (user.userid == "999999" && pwd == "999999")
            {
                user.power = "999";
                return true;
            }

            int number = -1;
            if (!int.TryParse(usrid, out number))
            {
                sError = "员工号需要数字!";
                return false;
            }

            try
            {
                SqlParameter param_id = new SqlParameter("@id", usrid);
                string sSql = string.Format("select * from [{0}] where UserID = @id", m_sTableName);
                DataTable dtUser = MicroDBHelper.ExecuteDataTable(sSql, new SqlParameter[] { param_id }, m_sConnect);
                if (dtUser == null || dtUser.Rows.Count == 0)
                {
                    sError = string.Format("员工号{0}不存在!", usrid);
                    return false;
                }

                if (dtUser.Rows[0][clsUser.chn_Pwd].ToString() == pwd)
                {
                    user.userid = dtUser.Rows[0][clsUser.chn_UserID].ToString();
                    user.user = dtUser.Rows[0][clsUser.chn_User].ToString();
                    user.password = dtUser.Rows[0][clsUser.chn_Pwd].ToString();
                    user.power = dtUser.Rows[0][clsUser.chn_Power].ToString();
                    user.remark = dtUser.Rows[0][clsUser.chn_Remark].ToString();
                    return true;
                }

                sError = string.Format("员工号{0},密码错误!!", usrid);
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show("服务器异常!");
                sError = "服务器异常!" + ex.Message;
            }

            return false;
        }

        public bool HavePower(clsPowerManage.POWER_FEAFTURES power)
        {
            return clsPowerManage.getPower(this, (int)power);
        }

        public bool HavePower(clsPowerManage.POWER_FEAFTURES_MOD power)
        {
            return clsPowerManage.getPower(this, (int)power);
        }

        public bool ChangePassword(string strOld, string strNew)
        {
            if (verifyUser(sUserID, strOld))
            {
                try
                {
                    string sSql = string.Format("update {0} set [Password] = '{1}' where [UserID] = {2}",
                                         m_sTableName, strNew, sUserID);
                    if (MicroDBHelper.ExecuteNonQuery(sSql, null, m_sConnect) == 1)
                    {
                        return true;
                    }
                }
                catch { }
            }

            return false;
        }

        public bool ChangePassword1(string userID, string strOld, string strNew)
        {
            clsUser us = new clsUser(m_sConnect);
            if (verifyUser1(userID, strOld, us))
            {
                try
                {
                    string sSql = string.Format("update {0} set [Password] = '{1}' where [UserID] = {2}",
                                         m_sTableName, strNew, userID);
                    if (MicroDBHelper.ExecuteNonQuery(sSql, null, m_sConnect) == 1)
                    {
                        return true;
                    }
                }
                catch { }
            }

            return false;
        }
    }

    public class clsUser
    {
        public string m_sConnect;
        public int iColumnNum;
        public string[] m_arrDbHead, m_arrExcelHead;

        public string userid;
        public string user;
        public string password;
        public string power;
        public string remark;

        public static string chn_ID = "ID";
        public static string chn_UserID = "UserID";
        public static string chn_User = "User";
        public static string chn_Pwd = "Password";
        public static string chn_Power = "Power";
        public static string chn_Remark = "Remark";

        public clsUser(string sConnect)
        {
            m_sConnect = sConnect;
            user = "---";
            userid = "---";
            password = "";
            power = "";//权限
            remark = "产品检验员";

            iColumnNum = 6;
            m_arrDbHead = new string[iColumnNum];
            int i = 0;
            m_arrDbHead[i++] = "[" + chn_ID + "]";//
            m_arrDbHead[i++] = "[" + chn_UserID + "]";
            m_arrDbHead[i++] = "[" + chn_User + "]";//
            m_arrDbHead[i++] = "[" + chn_Pwd + "]";//
            m_arrDbHead[i++] = "[" + chn_Power + "]";//
            m_arrDbHead[i++] = "[" + chn_Remark + "]";//

            m_arrExcelHead = new string[iColumnNum];
            i = 0;
            m_arrExcelHead[i++] = "序号";
            m_arrExcelHead[i++] = "员工号";
            m_arrExcelHead[i++] = "用户名";
            m_arrExcelHead[i++] = "密码";
            m_arrExcelHead[i++] = "权限";
            m_arrExcelHead[i++] = "备注";

            //    CREATE TABLE tb_User (
            //    [ID] [int] IDENTITY(1,1) NOT NULL,
            //    [UserID] INT NOT NULL,
            //    [User] NVARCHAR(50) NOT NULL,
            //    [Password] NVARCHAR(50) NOT NULL,
            //    [Power] NVARCHAR(50) NULL DEFAULT NULL,
            //    [Remark] NVARCHAR(50) NULL DEFAULT NULL,
            //)
            //;
        }

        public string GetDbStr(int iStart)
        {
            if (iStart >= m_arrDbHead.Length)
            {
                return "";
            }
            StringBuilder sbDb = new StringBuilder(256);

            for (int i = iStart; i < m_arrDbHead.Length; i++)
            {
                sbDb.Append(m_arrDbHead[i]);
                if (i != m_arrDbHead.Length - 1)
                {
                    sbDb.Append(", ");
                }
            }
            return sbDb.ToString();
        }
    }

    public class clsPowerManage
    {
        public enum POWER_FEAFTURES : int
        {
            //超级权限
            ADMIN = 999,

            //互调测试
            PIM = 1,
            //互调手动上传
            PIM_MANUAL = 2,
            //互调配置
            PIM_CONFIG = 3,
            //互调调试
            PIM_DEBUG = 4,
            //互调
            PIM_CONFIG_SELECT = 10,
            //仪表配置信息
            PIM_CalInstruments = 11,

            //电性能
            ELEC = 5,
            //电性能手动上传
            ELEC_MANUAL = 6,
            //互调配置
            ELEC_CONFIG = 7,
            //电性能特采
            ELEC_CONFIG_SELECT = 8,
            //电性能调试
            ELEC_DEBUG = 9,
        }

        public enum POWER_FEAFTURES_MOD : int
        {
            //超级权限
            ADMIN = 999,

            //互调测试
            TEST = 1,
            //互调手动上传
            ENG = 2,
            //互调配置
            CONFIG = 3,
            //互调设备配置
            //PIM_DEVICE = 4,
            //互调调试
            SETUP = 4,
            //校准
            CALIBRATION = 5
        }

        public static bool getPower(ClsDataUser user, int power)
        {
            if (GDefine.IS_USER_ALL_ADMIN) return true;

            try
            {
                string[] lstPower = user.aUser.power.Split(',');
                foreach (var p in lstPower)
                {
                    int iPower = Convert.ToInt32(p);
                    if (iPower == 999 || iPower == (int)power)
                        return true;
                }

                string[] lstPower2 = user.aUser2.power.Split(',');
                foreach (var p in lstPower2)
                {
                    int iPower = Convert.ToInt32(p);
                    if (iPower == 999 || iPower == (int)power)
                        return true;
                }
            }
            catch { }

            return false;
        }

        public static string getInfomation(bool isMod)
        {
            string info = "";

            if (!isMod)
            {
                info = "权限说明如下，格式为数字，多个权限请用逗号分隔（例： 1,2,3,4,999）" + "\r\n" + "\r\n" +
                          "超级权限:\t\t999" + "\r\n" +
                          "互调测试:\t\t1" + "\r\n" +
                          "互调手动上传数据:\t2" + "\r\n" +
                          "互调添加配置文件:\t3" + "\r\n" +
                          "互调DEBUG:\t\t4" + "\r\n" +
                          "互调配置文件可选:\t10" + "\r\n" +
                          "互调仪表配置信息:\t11" + "\r\n" +
                          "电性能测试:\t\t5" + "\r\n" +
                          "电性能手动上传数据:\t6" + "\r\n" +
                          "电性能添加配置文件:\t7" + "\r\n" +
                          "电性能配置文件可选:\t8" + "\r\n" +
                          "电性能DEBUG:\t\t9";

                //标签打印软件 100登录  101特权 
                //气密测试 200                
            }
            else
            {
                info = "权限说明如下，格式为数字，多个权限请用逗号分隔（例： 1,2,3,4,999）" + "\r\n" + "\r\n" +
                          "超级权限:\t999" + "\r\n" +
                          "测试模式:\t1" + "\r\n" +
                          "工程模式:\t2" + "\r\n" +
                          "    配置:\t3" + "\r\n" +
                          "    设置:\t4" + "\r\n" +
                          "    校准:\t5";
            }

            return info;
        }
    }

    public class ClsDataProduct
    {
        //条码
        public string sProductSN;
        //产品代码
        public string sProcode;
        //产品名称
        public string sProcodeName;
        //产品状态标识
        public string sPIMTestStep;
        //本地标识
        public string sLocalStep;
        //产品批次号(天线)
        public string sMOID;
        //敲击标识
        public bool isKnock;
        //调度速度
        public int iSpeed;
        //是否上传
        public bool isUploadResult;
        //重置角度，0为最小，1为最大
        public int iRebootStatus;
        //eng mode
        public int iMode;
        //enable列表天线部门使用，射频部门直接使用配置里的enable字段
        public List<bool> enableConfig;
        public ClsDataRcuConfig rcuConfig;
        public List<bool> enableTestItem;
        public string sConfigVer;
    }

    public class ClsDataSpecial
    {
        public bool bEnable;
        public bool bNeedCheckPIM;
        public float fMaxVaule;
        public float fMinVaule;

        public void SetDataSpecial(bool enable, bool needPIM, float max, float min)
        {
            this.bEnable = enable;
            this.bNeedCheckPIM = needPIM;
            this.fMaxVaule = max;
            this.fMinVaule = min;
        }
    }
}
